﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Versioning;
using System.ServiceProcess;
using System.Threading;

namespace PLM.ConfigurationCenter.Handler {

    public class ServiceManage {

        //安装服务
        public static void ServiceInstall(string serviceFilePath, string ServerName, bool CustomInstallGrammar = false, params string[] arguements) {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //不使用shell启动
            p.StartInfo.RedirectStandardInput = true;//喊cmd接受标准输入
            p.StartInfo.RedirectStandardOutput = false;//不想听cmd讲话所以不要他输出
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.Start();
            if (!CustomInstallGrammar)
                p.StandardInput.WriteLine($"sc.exe create \"{ServerName}\" binpath= \"{serviceFilePath}\" start= auto &exit");
            else
                p.StandardInput.WriteLine($"{serviceFilePath} {string.Join(" ", arguements)} &exit");
            p.StandardInput.AutoFlush = true;
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();

        }
        //安装服务
        public static void ServiceInstall(string serviceFilePath, string ServerName) {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //不使用shell启动
            p.StartInfo.RedirectStandardInput = true;//喊cmd接受标准输入
            p.StartInfo.RedirectStandardOutput = false;//不想听cmd讲话所以不要他输出
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.Start();
            p.StandardInput.WriteLine($"sc.exe create \"{ServerName}\" binpath= \"{serviceFilePath}\" start= auto &exit");
            p.StandardInput.AutoFlush = true;
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();

        }
        //卸载服务
        public static void ServiceUninstall(string serviceFilePath, string ServerName, bool CustomInstallGrammar = false, params string[] arguements) {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //不使用shell启动
            p.StartInfo.RedirectStandardInput = true;//喊cmd接受标准输入
            p.StartInfo.RedirectStandardOutput = false;//不想听cmd讲话所以不要他输出
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.Start();
            if (!CustomInstallGrammar)
                p.StandardInput.WriteLine($"sc.exe delete \"{ServerName}\" &exit");
            else
                p.StandardInput.WriteLine($"{serviceFilePath} {string.Join(" ", arguements)} &exit");
            p.StandardInput.AutoFlush = true;
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();
        }
        //卸载服务
        public static void ServiceUninstall(string ServerName) {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //不使用shell启动
            p.StartInfo.RedirectStandardInput = true;//喊cmd接受标准输入
            p.StartInfo.RedirectStandardOutput = false;//不想听cmd讲话所以不要他输出
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.Start();
            p.StandardInput.WriteLine($"sc.exe delete \"{ServerName}\" &exit");
            p.StandardInput.AutoFlush = true;
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();

        }
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public static bool ServiceStart(string serviceName) {
            var sc = GetService(serviceName);
            if (sc != null && sc.Status == ServiceControllerStatus.Stopped) {
                sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
            }
            bool state = sc != null && sc.Status == ServiceControllerStatus.Running;
            return state;
        }
        //停止服务
        public static bool ServiceStop(string serviceName) {
            var sc = GetService(serviceName);
            if (sc != null && sc.Status == ServiceControllerStatus.Running) {
                sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
            }
            bool state = sc != null && sc.Status == ServiceControllerStatus.Stopped;
            return state;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public static ServiceController GetService(string serviceName) {
            return ServiceController.GetServices().FirstOrDefault(sc => serviceName == sc.ServiceName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ServerName"></param>
        public static string ExecuteCmd(string cmdstr) {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //不使用shell启动
            p.StartInfo.RedirectStandardInput = true;//喊cmd接受标准输入
            p.StartInfo.RedirectStandardOutput = false;//不想听cmd讲话所以不要他输出
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.Start();
            p.StandardInput.WriteLine(cmdstr);
            //获取输出信息
            string strOuput = p.StandardOutput.ReadToEnd();
            p.StandardInput.AutoFlush = true;
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();
            return strOuput;
        }
    }
}
